﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExeLoaderTest
{
    using System.IO;
    using System.IO.Pipes;
    using System.Security.Principal;
    using System.Diagnostics;
    class Program
    {
        static void Main(string[] args)
        {
            if (SendData() == false)
            {
                Console.WriteLine("hello world, will close");
                Environment.Exit(1);
            }
            else
            {

                Process process = Process.GetCurrentProcess();

                Console.WriteLine(process.Id);


                Console.WriteLine("hello world");
            }

            Console.ReadLine();
        }
        private static bool SendData()
        {
            bool result = false;
            try
            {
                using (NamedPipeClientStream pipeClient =
              new NamedPipeClientStream("localhost", "ExeLoaderc5ad76de34ddPipe", PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.None))
                {
                    pipeClient.Connect(100);




                    using (StreamWriter sw = new StreamWriter(pipeClient))
                    {
                        while (pipeClient.IsConnected)
                        {
                            try
                            {
                                Console.WriteLine("hh ");

                                sw.WriteLine("hahha");

                                sw.Flush();


                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.ToString());

                                Environment.Exit(1);
                            }
                            System.Threading.Thread.Sleep(1000);
                        }

                    }
                    pipeClient.Close();
                    result = true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());

                result = false;
                //throw ex;
            }
            return result;
        }

    }
}
